<?php
//分页程序
function multi($num, $perpage, $curpage, $mpurl, $maxpages = 0) {
	$multipage = '';
	$mpurl .= strpos($mpurl, '?')!==false ? '&amp;' : '?';
	if($num > $perpage) {
		$page = 10;
		$offset = 2;

		$realpages = @ceil($num / $perpage);
		$pages = $maxpages && $maxpages < $realpages ? $maxpages : $realpages;

		if($page > $pages) {
			$from = 1;
			$to = $pages;
		} else {
			$from = $curpage - $offset;
			$to = $from + $page - 1;
			if($from < 1) {
				$to = $curpage + 1 - $from;
				$from = 1;
				if($to - $from < $page) {
					$to = $page;
				}
			} elseif($to > $pages) {
				$from = $pages - $page + 1;
				$to = $pages;
			}
		}

		$multipage = ($curpage - $offset > 1 && $pages > $page ? '<a class="p_redirect" href="'.$mpurl.'page=1">上一页</a>' : '').
			($curpage > 1 ? '<a  href="'.$mpurl.'page='.($curpage - 1).'">上一页</a>' : '');
		for($i = $from; $i <= $to; $i++) {
			$multipage .= $i == $curpage ? '<a class="current">'.$i.'</a>' :
				'<a href="'.$mpurl.'page='.$i.'">'.$i.'</a>';
		}

		$multipage .= ($curpage < $pages ? '<a class="p_redirect" href="'.$mpurl.'page='.($curpage + 1).'">下一页</a>' : '').
			($to < $pages ? '<a class="p_redirect" href="'.$mpurl.'page='.$pages.'">&rsaquo;|</a>' : '').
			($pages > $page ? '<a  ></a>' : '');
            
				
		$multipage = $multipage ? '<div class="m clearfix air"> <div class="pagin fr">'.$multipage.'</div> </div>' : '';
	}
	return $multipage;
}

?>
